package com.tarena.lbs.content.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
	
	// 默认分页参数
	private static final int DEFAULT_PAGE_NUM = 1;
	private static final int DEFAULT_PAGE_SIZE = 10;
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	// 分页查询文章分类列表
	@Override
	public PageResult<ArticleCategoryVO> selectPageList() {
		log.debug(">>>分页查询文章分类列表-业务");
		// 分页查询
		PageHelper.startPage(DEFAULT_PAGE_NUM, DEFAULT_PAGE_SIZE);
		List<ArticleCategoryPO> poList = categoryMapper.selectPageList();
		PageInfo<ArticleCategoryPO> pageInfo = new PageInfo<>(poList);
		// 转换数据，封装VO列表
		List<ArticleCategoryVO> voList = null;
		if (CollectionUtils.isNotEmpty(poList)) {
			voList = poList.stream().map(po -> {
				ArticleCategoryVO vo = new ArticleCategoryVO();
				BeanUtils.copyProperties(po, vo);
				return vo;
			}).toList();
		}
		// 封装分页结果
		PageResult<ArticleCategoryVO> pageResult = new PageResult<>();
		pageResult.setPageNo(pageInfo.getPageNum());
		pageResult.setPageSize(pageInfo.getPageSize());
		pageResult.setTotal(pageInfo.getTotal());
		pageResult.setObjects(voList);
		// 返回
		return pageResult;
	}
	
}
